<form id="formEdit" action="ep/save" class="form-horizontal" role="form" >
    <div class="modal-dialog  modal-md" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">操作</h4>
            </div>
            <div class="modal-body">

            	<!-- 以下为要修改的的内容 -->

				<input type="hidden" name="id" data-th-value="${model==null?'':model.id}">
				 
				<div class="row">
					<div class="form-group col-sm-6">
							<label for="txt_year" class="col-sm-5 control-label"><span
								class="text-red">*</span>选择年级</label>
							<div class="col-sm-7">
								<select id="year" name="year" class="form-control" onchange="selectCollege(this.value)">
								<option style="display: none"></option>
								<option data-th-each="years:${years}" data-th-text="${years}" data-th-value="${years}">1</option>
							   </select>
							</div>
					</div>
					<div class="form-group col-sm-6">
							<label for="txt_college" class="col-sm-5 control-label"><span
								class="text-red">*</span>选择学院</label>
							<div class="col-sm-7">
								<select id="college" name="college" class="form-control" onchange="selectProfesion(this.value)">
								<option style="display: none"></option>
							   </select>
							</div>
					</div>

				</div>
 
                <div class="row">
	                <div class="form-group col-sm-6">
							<label for="txt_profesion" class="col-sm-5 control-label"><span
								class="text-red">*</span>选择专业</label>
							<div class="col-sm-7">
								<select id="profesion" name="profesion" class="form-control" onchange="selectBanjiAndKc(this.value)">
								<option style="display: none"></option>
								</select>
							</div>
					</div>
					<div class="form-group col-sm-6">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>学年学期</label>
						<div class="col-sm-7">
						    <select id="xnxq" name="xnxqid" class="form-control" onchange="getkc(this.value)">
							</select>
						</div>
				    </div>
					
				</div>
				<div class="row">
					<div class="form-group col-sm-6">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>选择课程</label>
						<div class="col-sm-7">
						    <select id="kc" name="kcid"  onchange="cycle(this.value)" class="form-control">
							</select>
						</div>
					</div>
					<div class="form-group col-sm-6">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>选择班级</label>
						<div class="col-sm-7">
						    <select id="banji" name="banji" class="form-control" data-live-search="false" 
						    multiple="multiple">
							</select>
						</div>
					</div>
				</div>
				<div class="row"> 
					<div class="form-group col-sm-6">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>上课教师</label>
						<div class="col-sm-7">
						    <select id="teacher" name="teacher" class="form-control">
						    <option style="display: none"></option>
							<option data-th-each="teacher:${teacher}" data-th-text="${teacher.name}" data-th-value="${teacher.id}">1</option>
							</select>
						</div>
					</div>
					<div class="form-group col-sm-6">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>教室类型</label>
						<div class="col-sm-7">
						   <select id="jslx" name="jslx" class="form-control">
								<option style="display: none"></option>
								<option data-th-each="jslx:${jslx}" data-th-text="${jslx.name}" data-th-value="${jslx.name}">1</option>
						   </select>
						</div>
					</div>
				</div>
				
			<div class="row"> 
				<div class="form-group col-sm-6">
						<label for="txt_college" class="col-sm-5 control-label"><span
							class="text-red">*</span>开课周次</label>
						<div class="col-sm-7">
							<input class="form-control" id="qszcones" name="qszc" oninput="qszcone(this)" type="text" data-th-value="${model==null?'':model.qszc}">
						</div>
				</div>
				
				<div class="form-group col-sm-6">
						<label for="txt_professions" class="col-sm-5 control-label"><span
							class="text-red">*</span>结课周次</label>
						<div class="col-sm-7">
							<input class="form-control" id="jszcones" name="jszc" onchange="jszcone(this)" type="text" data-th-value="${model==null?'':model.jszc}">
						</div>
					</div>
					
					<div class="form-group col-sm-6">
						<label for="txt_xmh" class="col-sm-5 control-label"><span
							class="text-red">*</span>项目化课程</label>
						<div class="col-sm-7">
							<select name="xmh" class="form-control" id="xmh">
								<option style="display: none"></option>
								<option value="true" data-th-selected="${model==null?null:model.xmh!=null&&model.xmh}">是</option>
								<option value="false" data-th-selected="${model==null?null:model.xmh==null||!model.xmh}">否</option>
							</select>
						</div>
					</div>
			</div>
                <!-- 以上为要修改的内容 -->

            </div>
            <div class="modal-footer"> 
                <button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存</button>
                <button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭</button>
            </div>
        </div>
    </div>
</form>
<script type="text/javascript" th:inline="javascript">
		$("#year").selectpicker({
			title : '选择年级'
		});
		$("#college").selectpicker({
			title : '选择学院'
		});
		$("#profesion").selectpicker({
			title : '选择专业'
		});
		$("#banji").selectpicker({
			title : '选择班级'
		});
		$("#kc").selectpicker({
			title : '选择课程'
		});
		$("#xnxq").selectpicker({
			title : '选择学年学期'
		});
		$("#teacher").selectpicker({
			title : '选择教师'
		});
		$("#jslx").selectpicker({
			title : '选择教室类型'
		});
		$("#xmh").selectpicker({
			title : '选择项目化'
		});

		function selectCollege(year,collegeVal,profesionVal,list,xnxq,kc) {
			$("#kc").find("option").remove();
			$('#kc').selectpicker('refresh');
			$("#banji").find("option").remove();
			$('#banji').selectpicker('refresh'); 
			if(year==null||year==""){
			}else{
			$.post('ep/getCollege', {
				year : year
			}, function(result) {
					$('#college,#profesion').html(
							'<option style="display: none"></option>');
			
					for (var i = 0; i < result.length; i++) {
						$('#college').append(
								"<option value='"+result[i]+"'>" + result[i]
										+ "</option>");
					}
					$('#college').selectpicker('refresh');
					$('#profesion').selectpicker('refresh');
					if(collegeVal!=null&&collegeVal!=undefined){
						$('#college').selectpicker('val',collegeVal);
						selectProfesion(profesionVal,list,kc);
						
					}
			})
		
				$.post('ep/getXnxq', {
					year : year
				}, function(result) {
						$('#xnxq').html(
								'<option style="display: none"></option>');
				
						for (var i = 0; i < result.length; i++) {
							$('#xnxq').append(
									"<option value='"+result[i].id+"'>" + result[i].xnxq
											+ "</option>");
						}
						$('#xnxq').selectpicker('refresh');
						if(xnxq!=null&&xnxq!=undefined){
							$('#xnxq').selectpicker('val',xnxq);
						}
						
				})
			}
				
			}
			
		
		function selectProfesion(profesionVal,list,kc) {
				var college = $('#college ').val();
				var year = $('#year').val();
				$.post('ep/getProfesion', {
					college : college,
					year : year
				},function(result) {
						$('#profesion').html(
								'<option style="display: none"></option>');
						for (var i = 0; i < result.length; i++) {
							$('#profesion').append(
									"<option value='"+result[i].profesion+"'>"
											+ result[i].profesion + "</option>");
						}
						$('#profesion').selectpicker('refresh');
						if(profesionVal!=null&&profesionVal!=undefined){
							$('#profesion').selectpicker('val',profesionVal);
							selectBanji(list,kc);
						}
				})
		
		}
		function selectBanji(list,kc) {
		var year = $('#year').val();
		var college = $('#college').val();
		var profesion = $('#profesion').val();
		if(college==null||year==null||profesion==null){
			
		}else{
			$.post('ep/getBanji', {
				college : college,
				year : year,
				profesion : profesion
			},function(result) {
							$('#banji').html('<option style="display: none"></option>');
							for (var i = 0; i < result.length; i++) {
								$('#banji').append(
										"<option value='"+result[i].id+"'>"
												+ result[i].name + "</option>");
							}
							$('#banji').selectpicker('refresh');
							if(list!=null&&list!=undefined){
								$('#banji').selectpicker('val',list);
							}
					})
		}
		
		
		}
		
		function getkc(xnxq) {
			var year = $('#year').val();
			var college = $('#college').val();
			var profesion = $('#profesion').val();
			$.post('ep/getKc', {
				college : college,
				year : year,
				profesion : profesion,
				xnxq : xnxq
			},function(result) {
							$('#kc').html('<option style="display: none"></option>');
							for (var i = 0; i < result.length; i++) {
								$('#kc').append(
										"<option value='"+result[i].id+"'>"
												+ result[i].kcmc + "</option>");
							}
							$('#kc').selectpicker('refresh');
					})
		}
		
		function editskcs(xnxq,college,year,profesion){
			$.post('ep/getKc', {
				college : college,
				year : year,
				profesion : profesion,
				xnxq : xnxq
			},function(result) {
							$('#kc').html('<option style="display: none"></option>');
							for (var i = 0; i < result.length; i++) {
								$('#kc').append(
										"<option value='"+result[i].id+"'>"
												+ result[i].kcmc + "</option>");
							}
							$('#kc').selectpicker('refresh');
							if(kc!=null&&kc!=undefined){
								$('#kc').selectpicker('val',kc);
							}
					})
		}
		function selectBanjiAndKc(){
			var year = $('#year').val();
			var college = $('#college').val();
			var profesion = $('#profesion').val();
			if(year!=""){
			$.post('ep/getBanji', {college : college,year : year,profesion : profesion},function(result) {
							
				            $('#banji').html('<option style="display: none"></option>');
							for (var i = 0; i < result.length; i++) {
								$('#banji').append(
										"<option value='"+result[i].id+"'>"
												+ result[i].name + "</option>");
							}
							$('#banji').selectpicker('refresh');
					})
			}
			var xnxq = $("#xnxq").val();
			if(xnxq!=null&&xnxq!=""){
				$('#kc').selectpicker('refresh');
				getkc(xnxq);
			}
		}
 	var options= {
 			excluded : [':disabled'],
			message: '验证不通过',
			feedbackIcons: {
                 valid: 'glyphicon glyphicon-ok',
                 invalid: 'glyphicon glyphicon-remove',
                 validating: 'glyphicon glyphicon-refresh'
             },
             fields: { 
            	 year : {
     				validators : {
     					notEmpty : {
     						message : '年级不能为空'
     					}
     				}
     			},
     			college : {
      				validators : {
      					notEmpty : {
      						message : '学院不能为空'
      					}
      				}
      			},
      			profesion : {
      				validators : {
      					notEmpty : {
      						message : '专业不能为空'
      					}
      				}
      			},
      			banji : {
      				validators : {
      					notEmpty : {
      						message : '班级不能为空'
      					}
      				}
      			},
      			kcid : {
      				validators : {
      					notEmpty : {
      						message : '课程不能为空'
      					}
      				}
      			},
      			xnxqid : {
      				validators : {
      					notEmpty : {
      						message : '学年学期不能为空'
      					}
      				}
      			},
      			teacher : {
      				validators : {
      					notEmpty : {
      						message : '老师不能为空'
      					}
      				}
      			},
      			jslx : {
      				validators : {
      					notEmpty : {
      						message : '教室类型不能为空'
      					}
      				}
      			},
      			qszc : {
      				validators : {
      					notEmpty : {
      						message : '起始周次不能为空'
      					}
      				}
      			},
      			jszc : {
      				validators : {
      					notEmpty : {
      						message : '结束周次不能为空'
      					}
      				}
      			}
             }
         };
        $('#formEdit').bootstrapValidator(options).on('success.form.bv', function(e) {
        	e.preventDefault();							   	//很重要没有它则会提交默认表单，不会做下面的ajax表单提交
            var fd = new FormData($("#formEdit")[0]); 
            var url=$('#formEdit').attr('action');
        	$.ajax({ 
              url: url,  
              type: "POST",  
              data: fd,  
              processData: false,  // 不处理数据  
              contentType: false,   // 不设置内容类型
              success:function(result){
            	  if(result.success){
	            	  $('#myModal').modal("hide");
            	  }else{
            		  bootbox.alert(result.msg);
            	  }
              },
              error:function(result){
            	  bootbox.alert("保存请求发送失败，请检查网络是否连通");
              }
            });  
    	});
    	
    	
    	

</script>
<script type="text/javascript">
	if([[${model!=null}]]){
		var year = '[[${model==null?null:model.curriculum.profession.year}]]';
		var collegeVal = '[[${model==null?null:model.curriculum.profession.college}]]';
		var profesionVal = '[[${model==null?null:model.curriculum.profession.profesion}]]';
		var kc = '[[${model==null?null:model.curriculum.id}]]';
		var teacher = '[[${model==null?null:model.teacher.id}]]';
		var xnxq = '[[${model==null?null:model.schoolyear.id}]]';
		var list = '[[${model==null?null:model.bjs}]]'.replace(/ /g, '').slice(1, -1).split(',');
		var jslx = '[[${model==null?null:model.jslx}]]';
		$('#year').selectpicker('val', year);
		$('#college').selectpicker('val', college);
		$('#profesion').selectpicker('val', profesion);
		selectCollege(year,collegeVal,profesionVal,list,xnxq,kc)
		$('#teacher').selectpicker('val', teacher);
		$('#jslx').selectpicker('val', jslx);
		editskcs(xnxq,collegeVal,year,profesionVal);
		cycle(kc);
	}else{
		var year = $('#search_year').val();
		var college = $('#search_college').val();
		var profesion = $('#search_profesion').val();
		var banji = $('#search_banji').val();
		$('#year').selectpicker('val', year);
		selectCollege(year,college,profesion,banji);
	}
	var sum=0;
	function cycle(value){
		$.post('ep/findQszc', {ids:value} , function(data){
			if(data=="no"){
				bootbox.alert("该课程没有总学时或周学时<br>无法安排课程！");
			}
			if(data!=null){
				sum=data;
			}
		});
	}
	function qszcone(value){
		var re = /[^\d]/g;
		if(sum!=0){
			var dou=Number(value.value)+Number(sum)-Number("1");
			$("#jszcones").val(dou);
		}else{
			bootbox.alert("请先选择课程");
			$("#qszcones").val("");
		}
	}
	
	function jszcone(value){
		var re = /[^\d]/g;
		if(sum!=0){
			var dou=Number(value.value)-Number(sum)-Number("1");
			if(dou<1){
				bootbox.alert("结课周次应大于课程安排的周次："+sum);
				$("#jszcones").val("");
			}else{
				$("#qszcones").val(dou);
			}
		}else{
			bootbox.alert("请先选择课程");
			$("#jszcones").val("");
		}
	}
</script>
